#include "eeprom.hpp"
#include "stdio.h"
#include "indicator.hpp"
#include "system.h"
M24C64::M24C64(Portable *portable){
    this->portable = portable;
}

bool M24C64::open(void){
    this->portable->busInit();
    return true;
}

bool M24C64::close(void){
    return true;
}

int M24C64::read(void *buff,unsigned int address,unsigned int count){
    LED *led;
    led = led->getInstance();
    unsigned char *data_ptr = reinterpret_cast<unsigned char*>(buff);
    int offset = 0x00;
    if ( this->portable == nullptr ) return 0;

    for ( offset = 0; offset < count; offset++ ){
        //printf("read cnt %d\r\n",offset);
        //printf("Read %%%.2f\r\n",((float)offset/(float)count)*100);
        led->set(!led->get());
        data_ptr[offset] = this->portable->readByte(address + offset);
    }
    return offset;
}
int M24C64::write(void *buff,unsigned int address,unsigned int count){
    LED *led;
    led = led->getInstance();
    unsigned char *data_ptr = reinterpret_cast<unsigned char*>(buff);
    int offset = 0x00;
    if ( this->portable == nullptr ) return 0;

    for ( offset = 0x00; offset < count; offset++ ){
        led->set(!led->get());
        //printf("Write %%%.2f\r\n",((float)offset/(float)count)*100);
        this->portable->writeByte(address + offset,data_ptr[offset]);
        System_delayMs(5);
    }
    return offset;
}

M24C64::Info M24C64::getInfo(void){
    Info info = {
        .capacity = 8192,
    };
    return info;
}

